package us.gaaoc.framework.webapp.backingbeans.attorney.actions;

import java.util.List;
import java.util.UUID;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import us.gaaoc.framework.dao.ModelFacade;
import us.gaaoc.framework.model.Attorney;
import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.CaseRecordStatus;
import us.gaaoc.framework.model.Court;
import us.gaaoc.framework.model.DocumentStatus;
import us.gaaoc.framework.model.Documents;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.PersonParticipants;
import us.gaaoc.framework.model.constants.Constants;
import us.gaaoc.framework.webapp.annotations.UserAction;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;

/**
 * This is the send to court button action.
 * 
 * @author artripa
 *
 */
@UserAction
public class SendToCourtAction extends BaseBackingBean {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public boolean isClerkModuleDisabled(String courtUuid) 
	{		
		
		Court court = this.getModelFacade().findCourtByUuid(courtUuid);
		
		return court.isThruClerksAuthority();

		/**
		 
		return court.equalsIgnoreCase("courtUuidChatham") ||
			court.equalsIgnoreCase("courtUuidTesting") || 
			court.equalsIgnoreCase("courtUuidClarke") || 
			court.equalsIgnoreCase("courtUuidCrisp") ||
			court.equalsIgnoreCase("courtUuidDodge") ||
			court.equalsIgnoreCase("courtUuidUpson") ||
			court.equalsIgnoreCase("courtUuidAugustaRichmond") ||
			court.equalsIgnoreCase("courtUuidPeach") ||
			court.equalsIgnoreCase("courtUuidWilkinson") ||
			court.equalsIgnoreCase("courtUuidBulloch") ||
			court.equalsIgnoreCase("courtUuidMeriwether");
		 */
	}
	
	public boolean isVendor(String vendorId,String courtUuid)
	{
		Court court = this.getModelFacade().findCourtByUuid(courtUuid);	
		return court.getCmsName().equalsIgnoreCase(vendorId);
	}
	
	/**
	 * The text of the button behind this action.
	 * @return The text
	 */
	public String getText(String court)
	{

		//if(!court.isClerkModuleEnabled()) {
		if(isClerkModuleDisabled(court))
		{
			return "Queue for Court Processing";
		}
		else
		{
			return "Submit to Clerk";
		}		
	}
	
	protected void associateAttorneyWithCase(ModelFacade mf,Person p, CaseRecord cr)
	{
		List<PersonParticipants> pp  = mf.getPersonParicipant(cr);
		for (PersonParticipants o : pp) {
			if (o.getRelationship().getCode() == 4 && !o.getPerson().getFirstName().equalsIgnoreCase("ff20da2c-8e5c-11dd-9b78-0016d3"))
			{
				Attorney a = new Attorney();
				a.setBarID(mf.getBarID(o.getPerson(), cr.getCourt()));
				a.setPerson(o.getPerson());
				o.getPerson().setAttorney(a);
				
				PersonParticipants pt = mf.getPersonParticipantByParticipantRelationshipCode(cr, 4).get(0);
				if(pt.getPerson().getFirstName().equalsIgnoreCase("ff20da2c-8e5c-11dd-9b78-0016d3") 
						|| pt.getPerson().getUuid().equalsIgnoreCase(p.getUuid()))
				{
					pt.setPerson(p);
				}
				return;
			}
		}
	}
	
	/**
	 * This function sends a case to the court.
	 * @param cr The case record to send.
	 * @param sender The person who is sending.
	 * @param mf The database object.
	 * @param comments 
	 */
	public void sendToCourt(String cr,Person sender,ModelFacade mf,String court, String comments)
	{		
	
		CaseRecord caseRecord = mf.findCaseRecordById(cr, court);
		//associateAttorneyWithCase(mf,sender,caseRecord);
		
		CaseRecordStatus newCRS = null;
		
		//if(!court.isClerkModuleEnabled()) {
		if(isClerkModuleDisabled(court))
		{
			
			for(Documents doc : caseRecord.getDocuments())
			{												
				if(doc.getStatus().getCode()!=2)
				{				
					
					if(!doc.getContentAsFileHandle().exists()){
						caseRecord.setStatus(mf.findCaseRecordStatusByCode(Constants.CaseRecordStatus.FAILED_TO_IMPORT));
						return;
					}
				}
			}
			
			if(isVendor("XMLLEGAL",court))
			{
				caseRecord.setFilingId(UUID.randomUUID().toString().replaceAll("-",""));
			}else{
				caseRecord.setFilingId(caseRecord.getUuid());
			}
			
			mf.updateCaseRecord(caseRecord, court);			
			
			newCRS = mf.findCaseRecordStatusByCode(21);
			
			for(Documents doc : caseRecord.getDocuments())
			{												
				//If it isn't imported, go ahead
				// and queue it for import.
				if(doc.getStatus().getCode()!=2)
				{									
					mf.updateDocumentStatus(
								doc.getUuid(),
								new DocumentStatus(16,"queuedForImport"));
				}
			}
		}
		else
		{
			newCRS = mf.findCaseRecordStatusByCode(1);
		}	
		
		mf.updateCaseRecordStatus(cr,newCRS,court);		
		mf.addCaseRecordHistory(cr, court, 1, sender,comments);		
	}
	
}
